package com.jack006.article.controller;

import com.jack006.api.config.RabbitMQConfig;
import com.jack006.api.config.RabbitMQDelayConfig;
import com.jack006.api.controller.user.HelloControllerApi;
import com.jack006.article.stream.StreamService;
import com.jack006.grace.result.GraceJSONResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageDeliveryMode;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * @Classname HelloController
 * @Description 接口实现类
 * @Date 2021-06-10 08:15
 * @Created by jack
 */
@RestController
@RequestMapping("producer")
public class HelloController implements HelloControllerApi {

    final static Logger logger = LoggerFactory.getLogger(HelloController.class);


    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Autowired
    private StreamService streamService;


    /**
     * RabbieMQ 的路由规则 routing key
     * display.*.* ->> * 代表一个占位符
     *    例如：
     *      display.do.download 匹配
     *      display.do.upload.done 不匹配
     * display.#  -->> 代表任意多个占位符
     */

    @GetMapping("/hello")
    public Object hello(){
//        rabbitTemplate.convertAndSend(RabbitMQConfig.EXCHANGE_ARTICLE,
//                "article.hello",
//                "这是从生成者发送的消息");

        rabbitTemplate.convertAndSend(RabbitMQConfig.EXCHANGE_ARTICLE,
                "article.publish.download.do",
                "1001");

        rabbitTemplate.convertAndSend(RabbitMQConfig.EXCHANGE_ARTICLE,
                "article.success.do",
                "1002");

        rabbitTemplate.convertAndSend(RabbitMQConfig.EXCHANGE_ARTICLE,
                "article.play",
                "1003");


        return GraceJSONResult.ok("hello jack");
    }


    @GetMapping("/delay")
    public Object delay(){

        MessagePostProcessor messagePostProcessor = new MessagePostProcessor() {
            @Override
            public Message postProcessMessage(Message message) throws AmqpException {
                // 设置消息的持久
                message.getMessageProperties()
                        .setDeliveryMode(MessageDeliveryMode.PERSISTENT);
                // 设置消息的延迟的时间
                message.getMessageProperties()
                        .setDelay(5000);

                return message;
            }
        };

        rabbitTemplate.convertAndSend(
                RabbitMQDelayConfig.EXCHANGE_DELAY,
                "delay.demo",
                "这是一条延时消息",
                messagePostProcessor
        );
        logger.info("生产者发送时间"+new Date());
        return "OK";
    }

    @GetMapping("stream")
    public GraceJSONResult stream(){
        // streamService.sendMsg();

        for (int i = 0; i < 10; i++) {
            streamService.eat("我吃了第"+(i+1)+"只饺子");
        }

        return GraceJSONResult.ok("sendMsg success");
    }

}
